const staticAssets = [
	'./lession7-3(tarbar).html',
	'./lession7-3-2.html',
    '/js/rem.js',
    '/js/border1px.js',
	'/libs/zepto/zepto.js',
	'/libs/zepto/event.js',
	'/libs/zepto/ajax.js',
	'/css/normalize.css',
	'/imgs/jd/tarbar-item1.png',
	'/imgs/jd/tarbar-item2.png',
	'/imgs/jd/tarbar-item3.png',
	'/imgs/jd/tarbar-item4.png',
	'/imgs/jd/tarbar-item5.png',
	'/imgs/jd/tarbar-item1-default.png',
	'/imgs/jd/tarbar-item2-active.png',
    '/fallback.json'
];

self.addEventListener('install', async event => {
	const cache = await caches.open('cache');
	cache.addAll(staticAssets);
});


self.addEventListener('fetch', event => {
	const req = event.request;
	event.respondWith(networkFirst(req));
});

async function networkFirst(req) {
	const cache = await caches.open('cache');
	try {
		const res = await fetch(req)
		cache.put(req, res.clone());
		return res;
	} catch (error) {
		// return await cache.match(req);
		// const await cache.match(req);
		const cachedResponse = await cache.match(req);
		return cachedResponse || 
            await caches.match('./fallback.json');
	}
}